package com.demo.modalframe3.dialogs;

import com.demo.modalframe3.Frame2;
import com.demo.modalframe3.listener.DialogCloseListener;
import com.demo.modalframe3.utils.GraphicsUtil;
import com.demo.modalframe3.utils.UIUtil;
import com.esotericsoftware.tablelayout.swing.Table;

import javax.swing.*;
import java.awt.*;

public class ApplicationModalDialog extends JDialog {

    private Frame2 frame;

    public ApplicationModalDialog(Frame2 frame) {
        super(frame, "Application模态对话框", ModalityType.APPLICATION_MODAL);

        this.frame = frame;
        initDialog();

        this.setMinimumSize(new Dimension(GraphicsUtil.Dialog_Width, GraphicsUtil.Dialog_Height));
        this.setLocationRelativeTo(this);
        this.addWindowListener(new DialogCloseListener(frame));
    }

    private void initDialog() {
        JButton button1 = new JButton("递增父窗口数值后关闭");
        button1.addActionListener(e -> {
            frame.increaseNumber();
            this.setVisible(false);
        });

        JButton button2 = new JButton("关闭");
        button2.addActionListener(e -> {
            this.setVisible(false);
        });

        Table contentTable = UIUtil.defaultTable();
        contentTable.addCell(button1).row();
        contentTable.addCell(button2);

        JPanel panel = (JPanel)getContentPane();
        panel.setLayout(new BorderLayout());
        panel.add(contentTable, BorderLayout.CENTER);
    }
}
